/** ###################################################################
**     Filename  : MMA7455.C
**     Project   : project_xgate
**     Processor : MC9S12XDT512BMFU
**     Compiler  : CodeWarrior HCS12X C Compiler
**     Date/Time : 5/1/2009, 1:30 AM
**     Contents  :
**         User source code
**
**     (c) Copyright UNIS, a.s. 1997-2008
**     UNIS, a.s.
**     Jundrovska 33
**     624 00 Brno
**     Czech Republic
**     http      : www.processorexpert.com
**     mail      : info@processorexpert.com
** ###################################################################*/

/* MODULE MMA7455 */
#include "MMA7455.h"
#include "integer.h"
 #include "my_types.h"
 #include "SPI1_ACCEL.h"
 #include "INT2.h"
 #include "ACCEL_CS.h"
 #include "fixed.h"
 #include "INT1_CAP.h"

signed char X,Y,Z;                              //Ion MMA7455L acceleration data (8Bits)
BYTE ion_reg[32];                        //Array to read content of Ion MMA7455L registers                     
signed short calib[3]; 
byte random=0;
byte random2=2;
byte VALUE_1G = 64;
byte mode = ION_MEASURE;

 
READ_REGXOUTL ReadRegXOUTL;              //Review XOUTL in Real-Time Debugger
READ_REGXOUTH ReadRegXOUTH;              //Review XOUTH in Real-Time Debugger
READ_REGYOUTL ReadRegYOUTL;              //Review YOUTL in Real-Time Debugger
READ_REGYOUTH ReadRegYOUTH;              //Review YOUTH in Real-Time Debugger
READ_REGZOUTL ReadRegZOUTL;              //Review ZOUTL in Real-Time Debugger
READ_REGZOUTH ReadRegZOUTH;              //Review ZOUTH in Real-Time Debugger
READ_REGXOUT8 ReadRegXOUT8;              //Review XOUT8 in Real-Time Debugger
READ_REGYOUT8 ReadRegYOUT8;              //Review YOUT8 in Real-Time Debugger
READ_REGZOUT8 ReadRegZOUT8;              //Review ZOUT8 in Real-Time Debugger
READ_REGSTATUS ReadRegSTATUS;            //Review STATUS in Real-Time Debugger
READ_REGDETSR ReadRegDETSRC;             //Review DETSRC in Real-Time Debugger
READ_REGTOUT ReadRegTOUT;                //Review TOUT in Real-Time Debugger
READ_REGRESERVED1 ReadRegRESERVED1;      //Review RESERVED1 in Real-Time Debugger
READ_REGI2CAD ReadRegI2CAD;              //Review I2CAD in Real-Time Debugger
READ_REGUSRINF ReadRegUSRINF;            //Review USRINF in Real-Time Debugger
READ_REGWHOAMI ReadRegWHOAMI;            //Review WHOAMI in Real-Time Debugger
READ_REGXOFFL ReadRegXOFFL;              //Review XOFFL in Real-Time Debugger
READ_REGXOFFH ReadRegXOFFH;              //Review XOFFH in Real-Time Debugger
READ_REGYOFFL ReadRegYOFFL;              //Review YOFFL in Real-Time Debugger
READ_REGYOFFH ReadRegYOFFH;              //Review YOFFH in Real-Time Debugger
READ_REGZOFFL ReadRegZOFFL;              //Review ZOFFL in Real-Time Debugger
READ_REGZOFFH ReadRegZOFFH;              //Review ZOFFH in Real-Time Debugger
READ_REGINTRST ReadRegINTRST;            //Review INTRST in Real-Time Debugger
READ_REGMCTL ReadRegMCTL;                //Review MCTL in Real-Time Debugger
READ_REGCTL1 ReadRegCTL1;                //Review CTL1 in Real-Time Debugger
READ_REGCTL2 ReadRegCTL2;                //Review CTL2 in Real-Time Debugger
READ_REGLDTH ReadRegLDTH;                //Review LDTH in Real-Time Debugger
READ_REGPDTH ReadRegPDTH;                //Review PDTH in Real-Time Debugger
READ_REGPW ReadRegPW;                    //Review PW in Real-Time Debugger
READ_REGLT ReadRegLT;                    //Review LT in Real-Time Debugger
READ_REGTW ReadRegTW;                    //Review TW in Real-Time Debugger
READ_REGRESERVED2 ReadRegRESERVED2;      //Review RESERVED2 in Real-Time Debugger

void MMA7455_writeReg(unsigned char Addr, unsigned char data){
   unsigned char temp;
   unsigned char myaddr;
   myaddr =  (Addr <<1) | 0x80;    //Setting Bit 7 high = Write
   
   ACCEL_CS_ClrVal();   //CS Going Low

   while(SPI1_ACCEL_SendChar(myaddr)==ERR_TXFULL){}  //Sending Address And Write Bit
   while(SPI1_ACCEL_RecvChar(&temp)==  ERR_RXEMPTY){} //collecting returned data
   
  
   while(SPI1_ACCEL_SendChar(data)==ERR_TXFULL){}  //Sending Dummy byte
   while(SPI1_ACCEL_RecvChar(&temp)==  ERR_RXEMPTY){} //collecting returned data
                                                      //Not going to use it

   ACCEL_CS_SetVal();   //CS going High!

}

char MMA7455_readReg(unsigned char Addr){
       unsigned char temp;  
       unsigned char myaddr;
   myaddr =  (Addr << 1) & 0x7F;     //Setting Bit 7 low = Read
   
   ACCEL_CS_ClrVal();   //CS Going Low

   while(SPI1_ACCEL_SendChar(myaddr)==ERR_TXFULL){}  //Sending Address And Write Bit
   while(SPI1_ACCEL_RecvChar(&temp)==  ERR_RXEMPTY){} //collecting returned data
   
   while(SPI1_ACCEL_SendChar(0xFF)==ERR_TXFULL){}  //Sending Dummy byte
   
   while(SPI1_ACCEL_RecvChar(&temp)==  ERR_RXEMPTY){} //collecting returned data
                                                     
   ACCEL_CS_SetVal();   //CS going High!
   
   return temp;
  
}


/**************************************************************
*	Function: 	Read_AccelerometerIon 
*	Parameters: None 
*	Return:		  Acceleration (8Bits) for X, Y and Z
*
* Note:       Acceleration data is signed byte (2's compliment) 
*             Conversion signed (2's compliment) to unsigned 
*             implemented.            	
**************************************************************/
void Read_AccelerometerIon(void)
{
  byte tmpbuff[3];
  byte res;
  
  
  	while(!((res=MMA7455_readReg(STATUS))& 0x01)); // wait for valid new data on DRDY signal
    tmpbuff[0] = MMA7455_readReg(XOUT8);                              
    tmpbuff[1] = MMA7455_readReg(YOUT8);                            
    tmpbuff[2] = MMA7455_readReg(ZOUT8);                          
  
  	ReadRegXOUT8.MergedBits.Reg=tmpbuff[0];  
  	ReadRegYOUT8.MergedBits.Reg=tmpbuff[1];
  	ReadRegZOUT8.MergedBits.Reg=tmpbuff[2];
  	
    X = tmpbuff[0];  //conversion signed to unsigned byte           
    Y = tmpbuff[1];  //conversion signed to unsigned byte        
    Z = tmpbuff[2];  //conversion signed to unsigned byte     
   
 }
 
 
 void MMA7455_getVector(vector_t *myVec){
  
  (*myVec).X = X;
  (*myVec).Y = Y;
  (*myVec).Z = Z;
  
 }

          /**************************************************************
*	Function: 	SetUp_ModeOfSensorIon 
*	Parameters: gsel (ION_G2, ION_G4 or ION_G8)  
*	Return:		  None
*
*	Note:       Set up or change mode for acceleration sensor 	
**************************************************************/
void SetUp_ModeOfSensorIon(byte gsel)
{
byte k;

	      /* Set up of Ion Registers for standby, measure, level and/or pulse */ 
        /********************************************************************/
        /*
        Set up MCTL
	      Bit[7]   -                        
        Bit[6]   DRDP       0 Data ready status is output to INT1/DRDY
                            1 Data ready status is not output to INT1/DRDY   
        Bit[5]   SPI3W      0 SPI is 4 wire mode 
                            1 SPI is 3 wire mode
        Bit[4]   STON       0 Self test is not enabled                   
                            1 Self test is enabled                           
        Bit[3:2] GLVL[1:0]  00 8g selected for measurement mode, detection is 8g range                    
                            01 2g selected for measurement mode, detection is 8g range 
                            10 4g selected for measurement mode, detection is 8g range                            
        Bit[1:0] MOD[1:0]   00 Standby Mode 
                            01 Measurement Mode
                            10 Level Detection Mode
                            11 Pulse Detection Mode        
                            
        Set up CTL1 
	      Bit[7]  DFBW        0 Digital filter band width is 62.5Hz                                                       
                            1 Digital filter band width is  125Hz                
        Bit[6]  THOPT       0 ABSOLUTE, LDTH is 7bits unsigned value
                            1 Pos./Neg., LDTH is 8bits signed value
                            Note: THOPT applies only to level detection   
        Bit[5]  ZDA         0 Z axis is enabled for detection                   
                            1 Z axis is disabled for detection                            
        Bit[4]  YDA         0 Y axis is enabled for detection                   
                            1 Y axis is disabled for detection                            
        Bit[3]  XDA         0 X axis is enabled for detection                   
                            1 X axis is disabled for detection                            
        Bit[2:1] INTGR[1:0] 00 INT1: Level detection / INT2: Pulse detection                             
                            01 INT1: Pulse detection / INT2: Level detection                        
                            10 INT1: Single Pulse detection / INT2: Pulse detection                                                           
        Bit[0]  INTPIN      0 INT1 is routed to INT1                            
                            1 INT1 is routed to INT2       
                            
        Set up CTL2 
        Bit[7:3] -
        Bit[2]   DRDV0 Drive strength SDA/SDO
        Bit[1]   PDPL  Pulse detection
        Bit[0]   LDPL  Motion detection
                       LDPL=0 Motion detection
                       LDPL=1 Free Fall               
                                                                                        */
	  

	  
	   Ion_SwitchMode(ION_MEASURE);                                            
     
     k = MMA7455_readReg(MCTL);
     k|=0x01;          //MODE[1:0]=01 (measurement mode)
	   k &= 0x93;        //DRDP=0 (data ready is output on to INT1/DRDY pin) and GLVL[1:0]=00 (8g)
	   k |= (gsel << 2); //GLVL[1:0]=gsel  
	   MMA7455_writeReg(MCTL,k);
	   ReadIonMCTL();
	   
	   MMA7455_writeReg(CTL1,0x00);    //This is to set up DFBW: 62.5Hz for data rate 125Hz 
	   //SwSPI_WriteByte(CTL1,0x80);  //This is to set up DFBW:  125Hz for data rate 250Hz                                                
	   ReadIonCTL1();                 
     
     MMA7455_writeReg(I2CAD,0x80); //disable iic ;
  	 mode = ION_MEASURE;
  	 
  	 //Read Ion Registers (view in Real-Time Debugger)
     ReadAllIonRegisters();    	
}


/**************************************************************
*	Function: 	SetUp_GselectIon 
*	Parameters: gsel (ION_G2, ION_G4 or ION_G8) 
*	Return:		  None
*
*	Note:       Set up g range (2g, 4g or 8g)	
**************************************************************/
void SetUp_GselectIon(byte gsel)
{
	byte i;
	
	i = MMA7455_readReg(MCTL);
	i &= ~0x0c;
	if(gsel > 2) gsel = 2;
	i |= (gsel << 2);
	MMA7455_writeReg(MCTL,i);
	ReadIonMCTL();	
}


/**************************************************************
*	Function: 	Ion_SwitchMode 
*	Parameters: mode (measure)  
*	Return:		  None
*
*	Note:       Set up measure mode	
**************************************************************/
void Ion_SwitchMode(byte mode)
{
  BYTE i;
 	i = MMA7455_readReg(MCTL);
	i &= ~0x03;
	if(mode > 3) mode = 0;
	i |= mode;
	MMA7455_writeReg(MCTL,i);
	ReadIonMCTL();
}

/**************************************************************
*	Function: 	AutoCalibration_Ion 
*	Parameters: None  
*	Return:		  Calibration 	 
*
*	Note:       No data retention in Ion MMA7455L at power off
*             requiring to recalibrate or reload calibration 
*             information 	
**************************************************************/
byte AutoCalibration_Ion(void)
{
  signed int tmpcalib[3];
  signed int tmpaccel[3];
  signed char tmpbuff[3]; 
  byte cnt_runs =0;
  byte res = 0;
 	byte i,j;
   INT1_CAP_DisableEvent();
  for(i=0;i<3;i++)
  { 
    tmpcalib[i] = 0;
  }
  
 	SetUp_GselectIon(ION_G2);

 	while((cnt_runs++ < MAX_CNT_RUNS) && (res == 0))
 	{
 	  for(i=0;i<3;i++) tmpaccel[i] = 0; // Clear 
 	  
 	  for(i=0;i<10;i++)
 	  {
 	    
 	    Read_AccelerometerIon();
  //Capture current Acceleration values
     tmpbuff[0] = X;
     tmpbuff[1] = Y;
      tmpbuff[2] = Z;

 	    for(j=0;j<3;j++) tmpaccel[j] += tmpbuff[j]; 	     
 	  }
 	  
 	  for(i=0;i<3;i++) tmpaccel[i] /= 10;
 	  
 	  tmpaccel[2] -= 0x3F;   //Sensitivity Z axis Ion adjustment 
 	                         // 0x3F is 1g at 2g range
 	  for(i=0;i<3;i++)
 	  {
 	    if(tmpaccel[i] == 0) res++;      
 	  }
 	  
 	  if(res >= 3) 
 	  {
 	    res = 1;
 	    for(j=0;j<3;j++)
 	      tmpaccel[j] += 1;
 	  } 	    
 	  else res = 0;
 	      
 	  for(i=0;i<3;i++) tmpcalib[i] -= tmpaccel[i];
 	  
 	  for(i=0;i<3;i++)
    {    	    
 	    MMA7455_writeReg(XOFFL + i*2, (byte) (((word)tmpcalib[i]) % 256));
 	    MMA7455_writeReg(XOFFH + i*2, (byte) (((word)tmpcalib[i]) / 256)); 	  
 	    calib[i] = tmpcalib[i];
 	  } 
 	  	  	  	      
 	}
 	
    INT1_CAP_EnableEvent();
	return(res); 
}


void StaticCalibration_Ion(){
  
  signed short vector[3];
  signed short read_in[3];
  signed short read_in2[3];
  signed short read_in3[3]; 
  signed short difference[3]; 
   signed short desired[3];
  short sumsquared[3];
  volatile short sum;
 volatile short magnitude;
  short normalize;
  short timeout;
  
  byte i;
  INT1_CAP_DisableEvent();
  Cpu_Delay100US(100);

   Ion_SwitchMode( ION_STANDBY);
   Ion_SwitchMode( ION_MEASURE);
       magnitude = 0;
      timeout = 50;
     for(i=0;i<3;i++){
      
     calib[i]= 0; 
     }
     
    while(!(magnitude >=30 && magnitude<=33) && timeout){
      
    
        Read_AccelerometerIon();
      //Capture current Acceleration values
      vector[0] = X;
      vector[1] = Y;
      vector[2] = Z;
     
      for(i=0;i<3;i++){
       sumsquared[i]= (unsigned short)vector[i]* (unsigned short)vector[i]; 
      }
      sum =  sumsquared[0] + sumsquared[1] + sumsquared[2];
      magnitude = Sqrt( sum);
      
      normalize = (31000)/ magnitude;
      
      for(i=0;i<3;i++){
        desired[i]= (((long)vector[i]*normalize)+500)/1000;
        difference[i] =    (desired[i] - vector[i]  );
        calib[i]+= difference[i] *2;
      }
      
       for(i=0;i<3;i++)
        {    	    
     	    MMA7455_writeReg(XOFFL + i*2, (byte) (((word)calib[i]) % 256));
     	    MMA7455_writeReg(XOFFH + i*2, (byte) (((word)calib[i]) / 256)); 	  
     	  } 
      sum = 0;
      timeout --;
    }
  
    sum = 0;
  INT1_CAP_EnableEvent();
  
}



/**************************************************************
*	Function: 	ReadAllIonRegisters 
*	Parameters: None   
*	Return:		 
*
*	Note:       Update Ion MMA7455L registers in 
*             Real-Time Debugger
**************************************************************/
void ReadAllIonRegisters(void) 
{
byte i;


 for(i=0;i<32;i++)
 {  
 ion_reg[i]=MMA7455_readReg(i);
 }


ReadRegXOUTL.MergedBits.Reg=ion_reg[0]; 
ReadRegXOUTH.MergedBits.Reg=ion_reg[1]; 
ReadRegYOUTL.MergedBits.Reg=ion_reg[2]; 
ReadRegYOUTH.MergedBits.Reg=ion_reg[3]; 
ReadRegZOUTL.MergedBits.Reg=ion_reg[4];  
ReadRegZOUTH.MergedBits.Reg=ion_reg[5];  
ReadRegXOUT8.MergedBits.Reg=ion_reg[6]; 
ReadRegYOUT8.MergedBits.Reg=ion_reg[7]; 
ReadRegZOUT8.MergedBits.Reg=ion_reg[8]; 
ReadRegSTATUS.MergedBits.Reg=ion_reg[9]; 
ReadRegDETSRC.MergedBits.Reg=ion_reg[10]; 
ReadRegTOUT.MergedBits.Reg=ion_reg[11];
ReadRegRESERVED1.MergedBits.Reg=ion_reg[12]; 
ReadRegI2CAD.MergedBits.Reg=ion_reg[13]; 
ReadRegUSRINF.MergedBits.Reg=ion_reg[14]; 
ReadRegWHOAMI.MergedBits.Reg=ion_reg[15]; 
ReadRegXOFFL.MergedBits.Reg=ion_reg[16]; 
ReadRegXOFFH.MergedBits.Reg=ion_reg[17]; 
ReadRegYOFFL.MergedBits.Reg=ion_reg[18]; 
ReadRegYOFFH.MergedBits.Reg=ion_reg[19]; 
ReadRegZOFFL.MergedBits.Reg=ion_reg[20]; 
ReadRegZOFFH.MergedBits.Reg=ion_reg[21];
ReadRegMCTL.MergedBits.Reg=ion_reg[22];  
ReadRegINTRST.MergedBits.Reg=ion_reg[23]; 
ReadRegCTL1.MergedBits.Reg=ion_reg[24]; 
ReadRegCTL2.MergedBits.Reg=ion_reg[25]; 
ReadRegLDTH.MergedBits.Reg=ion_reg[26];  
ReadRegPDTH.MergedBits.Reg=ion_reg[27];  
ReadRegPW.MergedBits.Reg=ion_reg[28]; 
ReadRegLT.MergedBits.Reg=ion_reg[29];  
ReadRegTW.MergedBits.Reg=ion_reg[30];
ReadRegRESERVED2.MergedBits.Reg=ion_reg[31];
}

/**************************************************************
*	Function: 	Set_SelfTest 
*	Parameters: None  
*	Return:		 
*
*	Note:       Self test enabled will deflect Z axis by +1g
**************************************************************/
void Set_SelfTest(void)
{
	byte i;
	
	i = MMA7455_readReg(MCTL);
	i |= 0b00010000;
	MMA7455_writeReg(MCTL,i);
	ReadIonMCTL();	
}

/**************************************************************
*	Function: 	Clear_SelfTest 
*	Parameters: None   
*	Return:		  None
*
*	Note:       Disable self test
**************************************************************/
void Clear_SelfTest(void)
{
	byte i;
	
	i = MMA7455_readReg(MCTL);
	i &= 0b11101111;
	MMA7455_writeReg(MCTL,i);
	ReadIonMCTL();	
}

/**************************************************************
*	Function: 	ReadIonXOUTL 
*	Parameters: None   
*	Return:		  None
*
*	Note:       Update XOUTL in Real-Time Debugger
**************************************************************/
void ReadIonXOUTL(void)
{
byte tmp;
tmp= MMA7455_readReg(XOUTL); 
ReadRegXOUTL.MergedBits.Reg=tmp; 
} 

/**************************************************************
*	Function: 	ReadIonXOUTH 
*	Parameters: None   
*	Return:		  None
*
*	Note:       Update XOUTH in Real-Time Debugger
**************************************************************/
void ReadIonXOUTH(void)
{
byte tmp;
tmp= MMA7455_readReg(XOUTH); 
ReadRegXOUTH.MergedBits.Reg=tmp; 
}  

/**************************************************************
*	Function: 	ReadIonYOUTL 
*	Parameters: None   
*	Return:		  None
*
*	Note:       Update YOUTL in Real-Time Debugger
**************************************************************/
void ReadIonYOUTL(void)
{
byte tmp;
tmp= MMA7455_readReg(YOUTL); 
ReadRegYOUTL.MergedBits.Reg=tmp; 
} 

/**************************************************************
*	Function: 	ReadIonYOUTH 
*	Parameters: None   
*	Return:		  None
*
*	Note:       Update YOUTH in Real-Time Debugger
**************************************************************/
void ReadIonYOUTH(void)
{
byte tmp;
tmp= MMA7455_readReg(YOUTH); 
ReadRegYOUTH.MergedBits.Reg=tmp; 
} 

/**************************************************************
*	Function: 	ReadIonZOUTL 
*	Parameters: None   
*	Return:		  None
*
*	Note:       Update ZOUTL in Real-Time Debugger
**************************************************************/
void ReadIonZOUTL(void)
{
byte tmp;
tmp= MMA7455_readReg(ZOUTL); 
ReadRegZOUTL.MergedBits.Reg=tmp; 
} 

/**************************************************************
*	Function: 	ReadIonZOUTH 
*	Parameters: None   
*	Return:		  None
*
*	Note:       Update ZOUTH in Real-Time Debugger
**************************************************************/
void ReadIonZOUTH(void)
{
byte tmp;
tmp= MMA7455_readReg(ZOUTH); 
ReadRegZOUTH.MergedBits.Reg=tmp; 
} 

/**************************************************************
*	Function: 	ReadIonXOUT8 
*	Parameters: None   
*	Return:		  None
*
*	Note:       Update XOUT8 in Real-Time Debugger
**************************************************************/
void ReadIonXOUT8(void)
{
byte tmp;
tmp= MMA7455_readReg(XOUT8); 
ReadRegXOUT8.MergedBits.Reg=tmp; 
} 

/**************************************************************
*	Function: 	ReadIonYOUT8 
*	Parameters: None   
*	Return:		  None
*
*	Note:       Update YOUT8 in Real-Time Debugger
**************************************************************/
void ReadIonYOUT8(void)
{
byte tmp;
tmp= MMA7455_readReg(YOUT8); 
ReadRegYOUT8.MergedBits.Reg=tmp; 
} 

/**************************************************************
*	Function: 	ReadIonZOUT8 
*	Parameters: None   
*	Return:		  None
*
*	Note:       Update ZOUT8 in Real-Time Debugger
**************************************************************/
void ReadIonZOUT8(void)
{
byte tmp;
tmp= MMA7455_readReg(ZOUT8); 
ReadRegZOUT8.MergedBits.Reg=tmp; 
} 

/**************************************************************
*	Function: 	ReadIonSTATUS 
*	Parameters: None   
*	Return:		  None
*
*	Note:       Update STATUS in Real-Time Debugger
**************************************************************/
void ReadIonSTATUS(void)
{
byte tmp;
tmp= MMA7455_readReg(STATUS); 
ReadRegSTATUS.MergedBits.Reg=tmp; 
}

/**************************************************************
*	Function: 	ReadIonDETSR 
*	Parameters: None   
*	Return:		  None
*
*	Note:       Update DETSR in Real-Time Debugger
**************************************************************/ 
void ReadIonDETSR(void)
{
byte tmp;
tmp= MMA7455_readReg(DETSRC); 
ReadRegDETSRC.MergedBits.Reg=tmp; 
} 

/**************************************************************
*	Function: 	ReadIonTOUT 
*	Parameters: None   
*	Return:		  None
*
*	Note:       Update TOUT in Real-Time Debugger
**************************************************************/
void ReadIonTOUT(void) 
{
byte tmp;
tmp= MMA7455_readReg(TOUT); 
ReadRegTOUT.MergedBits.Reg=tmp; 
} 

/**************************************************************
*	Function: 	ReadIonI2CAD 
*	Parameters: None   
*	Return:		  None
*
*	Note:       Update I2CAD in Real-Time Debugger
**************************************************************/
void ReadIonI2CAD(void)
{
byte tmp;
tmp= MMA7455_readReg(I2CAD); 
ReadRegI2CAD.MergedBits.Reg=tmp; 
} 

/**************************************************************
*	Function: 	ReadIonUSRINF 
*	Parameters: None   
*	Return:		  None
*
*	Note:       Update USRINF in Real-Time Debugger
**************************************************************/
void ReadIonUSRINF(void)
{
byte tmp;
tmp= MMA7455_readReg(USRINF); 
ReadRegUSRINF.MergedBits.Reg=tmp; 
} 

/**************************************************************
*	Function: 	ReadIonWHOAMI 
*	Parameters: None   
*	Return:		  None
*
*	Note:       Update WHOAMI in Real-Time Debugger
**************************************************************/
void ReadIonWHOAMI(void)
{
byte tmp;
tmp= MMA7455_readReg(WHOAMI); 
ReadRegWHOAMI.MergedBits.Reg=tmp; 
} 

/**************************************************************
*	Function: 	ReadIonXOFFL 
*	Parameters: None   
*	Return:		  None
*
*	Note:       Update XOFFL in Real-Time Debugger
**************************************************************/
void ReadIonXOFFL(void)
{
byte tmp;
tmp= MMA7455_readReg(XOFFL); 
ReadRegXOFFL.MergedBits.Reg=tmp; 
} 

/**************************************************************
*	Function: 	ReadIonXOFFH 
*	Parameters: None   
*	Return:		  None
*
*	Note:       Update XOFFH in Real-Time Debugger
**************************************************************/
void ReadIonXOFFH(void)
{
byte tmp;
tmp= MMA7455_readReg(XOFFH); 
ReadRegXOFFH.MergedBits.Reg=tmp; 
}

/**************************************************************
*	Function: 	ReadIonYOFFL  
*	Parameters: None   
*	Return:		  None
*
*	Note:       Update YOFFL in Real-Time Debugger
**************************************************************/
void ReadIonYOFFL(void)
{
byte tmp;
tmp= MMA7455_readReg(YOFFL); 
ReadRegYOFFL.MergedBits.Reg=tmp; 
} 

/**************************************************************
*	Function: 	ReadIonYOFFH 
*	Parameters: None   
*	Return:		  None
*
*	Note:       Update YOFFH in Real-Time Debugger
**************************************************************/
void ReadIonYOFFH(void)
{
byte tmp;
tmp= MMA7455_readReg(YOFFH); 
ReadRegYOFFH.MergedBits.Reg=tmp; 
}  

/**************************************************************
*	Function: 	ReadIonZOFFL 
*	Parameters: None   
*	Return:		  None
*
*	Note:       Update ZOFFL in Real-Time Debugger
**************************************************************/
void ReadIonZOFFL(void)
{
byte tmp;
tmp= MMA7455_readReg(ZOFFL); 
ReadRegZOFFL.MergedBits.Reg=tmp; 
} 

/**************************************************************
*	Function: 	ReadIonZOFFH 
*	Parameters: None   
*	Return:		  None
*
*	Note:       Update ZOFFH in Real-Time Debugger
**************************************************************/
void ReadIonZOFFH(void)
{
byte tmp;
tmp= MMA7455_readReg(ZOFFH); 
ReadRegZOFFH.MergedBits.Reg=tmp; 
}  

/**************************************************************
*	Function: 	ReadIonINTRST 
*	Parameters: None   
*	Return:		  None
*
*	Note:       Update INTRST in Real-Time Debugger
**************************************************************/
void ReadIonINTRST(void)
{
byte tmp;
tmp= MMA7455_readReg(INTRST); 
ReadRegINTRST.MergedBits.Reg=tmp; 
} 

/**************************************************************
*	Function: 	ReadIonMCTL 
*	Parameters: None   
*	Return:		  None
*
*	Note:       Update MCTL in Real-Time Debugger
**************************************************************/
void ReadIonMCTL(void)                  
{
byte tmp;
tmp= MMA7455_readReg(MCTL); 
ReadRegMCTL.MergedBits.Reg=tmp; 
}

/**************************************************************
*	Function: 	ReadIonCTL1 
*	Parameters: None   
*	Return:		  None
*
*	Note:       Update CTL1 in Real-Time Debugger
**************************************************************/
void ReadIonCTL1(void)                  
{
byte tmp;
tmp= MMA7455_readReg(CTL1); 
ReadRegCTL1.MergedBits.Reg=tmp; 
}

/**************************************************************
*	Function: 	ReadIonCTL2 
*	Parameters: None   
*	Return:		  None
*
*	Note:       Update CTL2 in Real-Time Debugger
**************************************************************/
void ReadIonCTL2(void)
{
byte tmp;
tmp= MMA7455_readReg(CTL2); 
ReadRegCTL2.MergedBits.Reg=tmp; 
}

/**************************************************************
*	Function: 	ReadIonLDTH 
*	Parameters: None   
*	Return:		  None
*
*	Note:       Update LDTH in Real-Time Debugger
**************************************************************/
void ReadIonLDTH(void)
{
byte tmp;
tmp= MMA7455_readReg(LDTH); 
ReadRegLDTH.MergedBits.Reg=tmp; 
}

/**************************************************************
*	Function: 	ReadIonPDTH 
*	Parameters: None  
*	Return:		  None
*
*	Note:       Update PDTH in Real-Time Debugger
**************************************************************/
void ReadIonPDTH(void)
{
byte tmp;
tmp= MMA7455_readReg(PDTH); 
ReadRegPDTH.MergedBits.Reg=tmp; 
} 

/**************************************************************
*	Function: 	ReadIonPW 
*	Parameters: None 
*	Return:		  None
*
*	Note:       Update PW in Real-Time Debugger
**************************************************************/
void ReadIonPW(void)
{
byte tmp;
tmp= MMA7455_readReg(PW); 
ReadRegPW.MergedBits.Reg=tmp; 
} 

/**************************************************************
*	Function: 	ReadIonLT 
*	Parameters: None   
*	Return:		  None
*
*	Note:       Update LT in Real-Time Debugger
**************************************************************/
void ReadIonLT(void)
{
byte tmp;
tmp= MMA7455_readReg(LT); 
ReadRegLT.MergedBits.Reg=tmp; 
} 

/**************************************************************
*	Function: 	ReadIonTW 
*	Parameters: None   
*	Return:		  None
*
*	Note:       Update TW in Real-Time Debugger
**************************************************************/
void ReadIonTW(void) 
{
byte tmp;
tmp= MMA7455_readReg(TW); 
ReadRegTW.MergedBits.Reg=tmp; 
}    

/* END MMA7455 */
